package com.meizi.experimentcode.thread;

/**
 * @author Administration
 * @date 2022/5/6 - 15:28 - 星期五
 * @project lambda
 * @package com.meizi.experimentcode.thread
 * @by 晨
 */
public class DeadLock implements Runnable{

    private Integer count = 1;
    private String lock = "lock";

    @Override
    public void run() {
        while (true) {
            count++;
            if (count % 2 == 0) {
                // 线程1需要获取lock锁 在获取a方法的this锁
                // 线程2需要获取this锁 在获取b方法里的lock锁
                synchronized (lock) {
                    a();
                }
            }else {
                synchronized (this) {
                    b();
                }
            }
        }
    }

    private synchronized void a() {
        System.out.println(Thread.currentThread().getName() + "a 方法。。。。");
    }

    private void b() {
        synchronized (lock){
            System.out.println(Thread.currentThread().getName() + "b 方法。。。。");
        }
    }

    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();
        Thread thread1 = new Thread(deadLock);
        Thread thread2 = new Thread(deadLock);
        thread1.start();
        thread2.start();
    }
}
